class JoystickController {
    constructor(interval=100,lst_functions={}) {
        this.running = false;
        this.intervalId = null;
        this.intervalId2 = null;
        this.interval=interval;
        this.lst_input_old=[];
        this.lst_input_new=[];
        this.lst_functions=lst_functions;
        this.lst_config={
            "1B17":"切换模式",
            "1B18":"切换模式",
            "1B15":"切换激光",
            "1B16":"切换激光",
            "0B6":"绝对变倍增加",
            "0B8":"绝对变倍减少",
            "0B7":"相对变焦增加",
            "0B9":"相对变焦减少",
            "0B0":"拍照",
            "0A9":"镜头移动",
            "0B1":"镜头回中",
            "0A1":"前后平移",
            "0A0":"左右平移",
            "0B10":"垂直上升",
            "0B12":"垂直下降",
            "0A5":"旋转",
            "1A0":"速度控制",
            "0B4":"起飞",
            "0B2":"返航",
            "0B5":"降落",
            "nBn":"函数的键",
        };
        this.getDeviceInput = this.getDeviceInput.bind(this);
        this.doEvent = this.doEvent.bind(this);
    }


    getDeviceInput() {
        let gamepads = navigator.getGamepads();
        let lst_input = [];
        for (let i = 0; i < gamepads.length; i++) {
            //获取摇杆设备
            const gamepad = gamepads[i];
            if (gamepad == null) {
                continue;
            }
            //找出正确的设备，设置gid
            if (gamepad.id == "Saitek Pro Flight X-56 Rhino Stick (Vendor: 0738 Product: 2221)") {
                gamepad.gid = "0";
            }
            if (gamepad.id == "Saitek Pro Flight X-56 Rhino Throttle (Vendor: 0738 Product: a221)") {
                gamepad.gid = "1";
            }
            //捕获按钮
            if (gamepad.gid != null) {
                //按钮
                for (let j = 0; j < gamepad.buttons.length; j++) {
                    const button = gamepad.buttons[j];
                    lst_input[gamepad.gid + "B" + j] = button.pressed;
                }
                for (let k = 0; k < gamepad.axes.length; k++) {
                    const axis = gamepad.axes[k];
                    lst_input[gamepad.gid + "A" + k] = axis;
                }
            }
        }
        this.lst_input_old=this.lst_input_new;
        this.lst_input_new=lst_input;
        //console.log(lst_input);
        return lst_input;
    }
    
    doEvent(){
        var val;
        var func_tmp;
        if(this.lst_input_new==[] || this.lst_input_old==[]){
            return;
        }
        for(var btn in this.lst_config){
            if((!(btn in this.lst_input_new)) || (!(btn in this.lst_input_old))){
                
                continue;
            }
            if(btn.indexOf('B') !== -1){
                if(this.lst_input_new[btn]==true && this.lst_input_old[btn]==false){
                    func_tmp=this.lst_functions[this.lst_config[btn]];
                    func_tmp();
                }
            }
            
            if(btn.indexOf('A') !== -1){
                if(btn=="0A9"){
                    val=this.lst_input_new[btn];
                    if(val<1.1){
                        if(val >0.9){
                            func_tmp=this.lst_functions[this.lst_config[btn]]["NW"];
                            func_tmp();
                        }else if(val >0.5){
                            func_tmp=this.lst_functions[this.lst_config[btn]]["W"];
                            func_tmp();
                        }else if(val >0.4){
                            func_tmp=this.lst_functions[this.lst_config[btn]]["SW"];
                            func_tmp();
                        }else if(val >0.1){
                            func_tmp=this.lst_functions[this.lst_config[btn]]["S"];
                            func_tmp();
                        }else if(val >-0.2){
                            func_tmp=this.lst_functions[this.lst_config[btn]]["SE"];
                            func_tmp();
                        }else if(val >-0.5){
                            func_tmp=this.lst_functions[this.lst_config[btn]]["E"];
                            func_tmp();
                        }else if(val >-0.8){
                            func_tmp=this.lst_functions[this.lst_config[btn]]["NE"];
                            func_tmp();
                        }else if(val >-1.1){
                            func_tmp=this.lst_functions[this.lst_config[btn]]["N"];
                            func_tmp();
                        }
                    }
                }else if(btn=="1A0"){
                    val=this.lst_input_new[btn];
                    func_tmp=this.lst_functions[this.lst_config[btn]];
                    func_tmp(val);
                }else {
                    val=this.lst_input_new[btn];
                    if (val<-0.2){
                        func_tmp=this.lst_functions[this.lst_config[btn]]["negative"];
                        func_tmp();
                    }
                    if (val>0.2){
                        func_tmp=this.lst_functions[this.lst_config[btn]]["positive"];
                        func_tmp();
                    }
                }
            }
        }
    }
    toggle(){
         if(this.running){
             this.running=false;
             clearInterval(this.intervalId);
             clearInterval(this.intervalId2);
             this.intervalId=null;
             this.intervalId2=null;
         }else{
             this.running=true;
             this.intervalId = setInterval(this.getDeviceInput, this.interval);
             this.intervalId2 = setInterval(this.doEvent, this.interval);
         }
        
    }

}
